local jieyun = fk.CreateSkill{
  name = "hanqing__jieyun",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["hanqing__jieyun"] = "皆云",
  [":hanqing__jieyun"] = "锁定技，若〖慧起〗无法发动，你视为装备着<a href=':hanqing__taigong_tactics'>【太公兵法】</a>。",
}

local function jieyunJudge(player)
  if not player:hasSkill("hanqing__huiqi") or player:usedSkillTimes("hanqing__huiqi", Player.HistoryGame) > 0 then
    return true
  else
    local card = Fk:cloneCard("slash")
    if table.contains(player:getTableMark("hanqing__huiqi"), 1) then
      card = Fk:cloneCard("stab__slash")
    end
    card.skillName = "hanqing__huiqi"
    return not player:canUse(card, { bypass_times = true })
  end
end

jieyun:addEffect("viewas", {
  pattern = ".|.|.|.|.|trick",
  prompt = function (self, player, selected_cards, selected)
    return "#hanqing__taigong_tactics_skill:::"..player:getMark("hanqing__taigong_tactics")
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if player:getMark("hanqing__taigong_tactics") == 0 then return end
    local card = Fk:cloneCard(player:getMark("hanqing__taigong_tactics"))
    card.skillName = "hanqing__taigong_tactics_skill&"
    return card
  end,
  before_use = function(self, player, use)
    player:drawCards(1, "hanqing__taigong_tactics_skill&")
  end,
  enabled_at_play = function(self, player)
    return player:isKongcheng() and jieyunJudge(player) and jieyunJudge(player) and player:getMark("hanqing__taigong_tactics") ~= 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and player:isKongcheng() and player:getMark("hanqing__taigong_tactics") ~= 0
  end,
  enabled_at_nullification = function (self, player, data)
    return player:isKongcheng() and player:getMark("hanqing__taigong_tactics") == "nullification" and jieyunJudge(player)
  end,
})

jieyun:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(jieyun.name, true) and data.card:isCommonTrick()
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "hanqing__taigong_tactics", data.card.name)
  end,
})

jieyun:addAcquireEffect(function (self, player, is_start)
  if not is_start then
    local room = player.room
    room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
      local use = e.data
      if use.from == player and use.card:isCommonTrick() then
        room:setPlayerMark(player, "hanqing__taigong_tactics", use.card.name)
        return true
      end
    end, -1)
  end
end)

jieyun:addAI(nil, "vs_skill")

return jieyun
